library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(p8105.datasets)
library(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
data("ny_noaa")
ny_noaa =
ny_noaa |>
drop_na() |>
mutate(
tmax = as.numeric(tmax),
tmin = as.numeric(tmin)
) |>
mutate(year = year(date))
Plotly plots
Scatterplot
ny_noaa |>
filter(year == 2002, month(date) %in% 6:9) |>
plot_ly(x = ~tmin, y = ~tmax, text = ~date, color = ~factor(month(date)),
alpha = .5, type = "scatter", mode = "markers", colors = "viridis") |>
layout(title = "tmax vs tmin (June to September 2002)",
xaxis = list(title = "tmax"),
yaxis = list(title = "tmin"),
coloraxis = list(title = "Month"))
Boxplot
ny_noaa |>
plot_ly(y = ~tmax, type = "box", name = "tmax") |>
add_trace(y = ~tmin, type = "box", name = "tmin") |>
layout(title = "tmax and tmin Distribution",
yaxis = list(title = "Temperature"))
Bar plot
ny_noaa |>
filter(year %in% 2001:2010) |>
group_by(year) |>
summarize(avg_snow = mean(snow, na.rm = TRUE)) |>
plot_ly(x = ~year, y = ~avg_snow, type = 'bar', name = 'Snowfall') |>
layout(title = "Average Snowfall (2001-2010)",
xaxis = list(title = "Year"),
yaxis = list(title = "Average Snowfall"))